-- soul_to_revive
-- created by chendh
-- 死亡时，可以消耗灵魂自动复活，并恢复一半生命

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 死亡触发
    trigger = function(who, prop, arg)

        if (who.dbase:query("soul_collect", 0) < prop[3]) then
            return FALSE;
        end

        local p = tonumber(prop[2]);
        local maxHp = who:getMaxHp();
        local hp = math.modf(maxHp * p / 1000);

        local baseHp = who.dbase:query("hp", 0);

        -- 需要滤掉属性加成的部分
        -- hp = hp - (who:getHp() - baseHp);

        -- 扣除灵魂
        who.dbase:set("soul_collect", who.dbase:query("soul_collect", 0) - prop[3]);
        -- 记录属性，增加下次的消耗
        PropM.record(who, {446, 500, 100, -1}, "soul");

        EventMgr.fire(event.SOUL_COST, {["amount"] = prop[3], ["attrib"] = "soul_collect" });

        -- 触发特技发动事件
        EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });

        -- 设置血量
        who:addAttrib("hp", hp - who:queryAttrib("hp"), "soul_revive");

        -- 同时恢复魔法
        if who.type == OBJECT_TYPE_USER then
            local maxMp = who:getMaxMp();
            local add_mp = math.modf(maxMp * 200 / 1000);
            -- 不能超过上限
            local requireMp = maxMp - who:queryAttrib("mp");
            add_mp = math.min(add_mp, requireMp);

            who:addAttrib("mp", add_mp, "soul_revive");

            -- 清除负面状态
            CombatStatusM.reviveClean(who);
        end

        return who:getHp() > 0;
    end,

    record = function(target, prop, path)
        return false;
    end
};
